package cn.lcl.web.servlet;

import cn.lcl.util.ProUtils;
import org.apache.log4j.Logger;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 所有的Servlet都应该继承这个类
 *
 */
public class BaseServlet extends HttpServlet {
    private static  final Logger log= Logger.getLogger(BaseServlet.class);

    protected static  final String REDIRECT="redirect:";

    protected  static String PREFIX="/WEB-INF/jsp/";//视图前缀

    protected  static  String SUBFFIX=".jsp";//视图后缀

    protected static  String defaultPage=REDIRECT+"/admin/index";
    @Override
    public void init() throws ServletException {
        String prefix=ProUtils.getProperty("prefix");
        String subffix=ProUtils.getProperty("subffix");
        if(prefix!=null)
            PREFIX=prefix;
        if(subffix!=null)
            SUBFFIX=subffix;
    }

    /**
     * 覆盖默认的服务方法。该方法通过分析链接来调用响应的方法来进行处理
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String servletPath=req.getRequestURI();
        String contextPath=req.getContextPath();
        if(!contextPath.equals("/"))//contextPath不为/
            servletPath=servletPath.substring(contextPath.length());
        log.info("服务端请求路径:"+servletPath);
        boolean isDefault=servletPath.indexOf("/",1)==-1 ? true :false;
//        log.info("是不是通过默认方式处理"+isDefault);
        if(isDefault){
            super.service(req,resp);
        }else {
            Integer index=servletPath.indexOf('?');//是否存在查詢字符串
            index=index==-1? servletPath.length() :index;
            String handMethod = servletPath.substring(servletPath.indexOf("/",1)+1,index);
            log.info(this.getClass().getName()+"调用"+handMethod+"()方法来处理请求");
            String result=defaultPage;//默认返回主页
            try {
                Method handle=this.getClass().getDeclaredMethod(handMethod,HttpServletRequest.class,HttpServletResponse.class);
                result=(String)handle.invoke(this,req,resp);
            } catch (NoSuchMethodException e) {
                log.info("不存在进行处理的方法");
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            //如果不需要跳转
            if(result==null)
                return;
            //客户端跳转
            if(result.contains(REDIRECT)){
                String finalView=req.getContextPath()+result.substring(REDIRECT.length());
                log.info("客户端跳转:"+finalView);
                resp.sendRedirect(finalView);
            } else {
                //服务器端跳转
                String finalView=PREFIX+result+SUBFFIX;//
                log.info("服务器端跳转到:"+finalView+"结果页");
                req.getRequestDispatcher(finalView).forward(req,resp);
            }
        }
    }
}
